package com.ptszyxx.popose.module.main.mine.vm;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.ptszyxx.popose.common.enums.ActionEnum;
import com.ptszyxx.popose.common.utils.YActionEventUtil;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ptszyxx.popose.common.utils.YBusUtil;
import com.ysg.utils.YChannelUtil;
import com.ysg.utils.YUserUtil;
import com.ysg.base.BaseViewModel;
import com.ysg.binding.command.BindingCommand;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.BaseResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnSuccessResult;
import com.ysg.http.data.CommonRepository;
import com.ysg.http.data.entity.mine.PayEntity;
import com.ysg.http.data.entity.mine.VipEntity;
import com.ysg.http.data.entity.mine.VipResult;
import com.ysg.http.data.entity.mine.VipUserEntity;
import com.ysg.utils.YCollectionUtil;
import com.ysg.utils.YPhoneUtil;
import com.ysg.utils.YStringUtil;
import java.util.HashMap;
import java.util.Map;
import io.reactivex.Observable;

public class MineVipVM extends BaseViewModel<CommonRepository> {
    public ObservableField<Boolean> privacyChecked = new ObservableField<>(false);
    public int currentPosition = 0;
    public VipEntity currentEntity;
    public VipResult vipResult;
    public String orderInfo;   // 订单信息

    public MineVipVM(@NonNull Application application, CommonRepository model) {
        super(application, model);
    }

    @Override
    public void requestUploadAction() {
        YActionEventUtil.getInstance().upload(this, model, ActionEnum.mine_vip);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onRefreshEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onRefreshItemEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onOtherEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onPayDialogEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onVipPrivilegeDialogEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onAlipayEvent = new SingleLiveEvent<>();
    }

    public BindingCommand onPayCommand = new BindingCommand(()-> uc.onPayDialogEvent.call());
    public BindingCommand onVip1Command = new BindingCommand(()-> uc.onVipPrivilegeDialogEvent.setValue("1"));
    public BindingCommand onVip2Command = new BindingCommand(()-> uc.onVipPrivilegeDialogEvent.setValue("2"));
    public BindingCommand onVip3Command = new BindingCommand(()-> uc.onVipPrivilegeDialogEvent.setValue("3"));
    public BindingCommand onVip4Command = new BindingCommand(()-> uc.onVipPrivilegeDialogEvent.setValue("4"));
    public BindingCommand onVip5Command = new BindingCommand(()-> uc.onVipPrivilegeDialogEvent.setValue("5"));
    public BindingCommand onVip6Command = new BindingCommand(()-> uc.onVipPrivilegeDialogEvent.setValue("6"));
    public BindingCommand onRechargeAgreementCommand = new BindingCommand(()-> YActivityUtil.getInstance().jumpRechargeAgreement(this));

    //=========================================接口：start===========================================
    /**
     * vip列表
     */
    public void requestVip(){
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .data(model.vip(map), this, new OnSuccessResult<VipResult>() {
                    @Override
                    public void onSuccessResult(BaseResponse<VipResult> response) {
                        vipResult = response.getData();
                        if (YCollectionUtil.isNotEmpty(response.getData().getList())){
                            currentEntity = response.getData().getList().get(0);
                            uc.onRefreshItemEvent.call();
                        }
                        uc.onRefreshEvent.setValue(response.getData().getList());
                        uc.onOtherEvent.call();

                        onVip();
                    }
                });
    }

    public void onItemClick(VipEntity entity, int position){
        this.currentPosition = position;
        this.currentEntity = entity;
        uc.onRefreshItemEvent.call();
    }

    /**
     * 支付
     */
    public void requestPay(String payType, String payId){
        Map<String, String> map = new HashMap<>();
        map.put("type", "2");
        map.put("projectId", payId);
        map.put("mobileSpec", YPhoneUtil.getModel());
        map.put("channel", YChannelUtil.getInstance().getChannel());

        Observable observable = model.payAlipay(map);
        if (YStringUtil.eq(2, payType)){
            observable = model.payWechat(map);
        }

        HttpUtils.getInstance()
                .data(observable, this, new OnSuccessResult<PayEntity>() {
                    @Override
                    public void onSuccessResult(BaseResponse<PayEntity> response) {
                        YActivityUtil.getInstance().jumpPay(MineVipVM.this, response.getData().getPayUrl(), payType);
                    }
                });
    }

    /**
     * 支付
     */
    public void requestPayApp(String payType, String payId){
        Map<String, String> map = new HashMap<>();
        map.put("type", "2");
        map.put("projectId", payId);
        map.put("mobileSpec", YPhoneUtil.getModel());
        map.put("channel", YChannelUtil.getInstance().getChannel());

        HttpUtils.getInstance()
                .data(model.payAlipayApp(map), this, new OnSuccessResult<String>() {
                    @Override
                    public void onSuccessResult(BaseResponse<String> response) {
                        orderInfo = response.getData();
                        uc.onAlipayEvent.call();
                    }
                });
    }

    /**
     * 判断是否vip
     * 1.本地保存
     * 2.通知“我的”页面刷新
     */
    private void onVip() {
        if (vipResult.getUser() == null){
            return;
        }
        VipUserEntity user = vipResult.getUser();
        YUserUtil.saveVip(user.getIsVip());
        YBusUtil.vip();
    }



}
